﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogic;
using Presentation.Children;
using Reporting;

namespace Presentation.Reports {
  /// <summary>
  /// Selection form for Chilfiche report
  /// </summary>
  public partial class ChildficheReportSelectionForm : Form {

    private ReportBll reportBll = new ReportBll( );
    private ChildrenBll childrenBll = new ChildrenBll( );
    private BusinessObjects.Children children = new BusinessObjects.Children( );

    #region - Constructor -

    /// <summary>
    /// Initializes a new instance of the <see cref="ChildficheReportSelectionForm"/> class.
    /// </summary>
    public ChildficheReportSelectionForm( ) {
      InitializeComponent( );
      activeRadioButton.Checked = true;
      searchButton.Enabled = false;
    }

    #endregion

    #region - Events -

    private void printButton_Click( object sender, EventArgs e ) {
      if ( detailCheckBox.Checked ) {
        CreateDetailReport( );
      } else {
        CreateReport( );
      }
    }

    private void searchButton_Click( object sender, EventArgs e ) {
      ChildrenListForm listForm = new ChildrenListForm( true /*multiselect = true*/ );
      if ( listForm.ShowDialog( ) == System.Windows.Forms.DialogResult.OK ) {
        children = listForm.SelectedChildren;
      }
    }

    private void selectionRadioButton_CheckedChanged( object sender, EventArgs e ) {
      searchButton.Enabled = selectionRadioButton.Checked;
    }

    #endregion

    #region - Methods -

    private void CreateDetailReport( ) {
      ChildficheDetailData detailData = new ChildficheDetailData( );
      if ( !selectionRadioButton.Checked ) {
        children = childrenBll.GetChildren( allRadioButton.Checked );
        detailData = reportBll.GetChildficheDetailReportData( children );
      } else {
        if ( children.Count == 0 ) {
          MessageBox.Show( "Geen selectie gemaakt.", "Selectie", MessageBoxButtons.OK, MessageBoxIcon.Warning );
        } else {
          detailData = reportBll.GetChildficheDetailReportData( children );
        }
      }
      if ( detailData.Children.Count > 0 ) {

        ReportViewForm viewForm = new ReportViewForm( detailData, "Children", "ChildficheDetailDataset", "Presentation.Reports.Childfiches.ChildficheDetailReport.rdlc" );
        viewForm.MdiParent = this.MdiParent;
        viewForm.Show( );
        this.Close( );
      } else {
        MessageBox.Show( "Geeg gegevens gevonden.", "Afdruk kinderfiches", MessageBoxButtons.OK, MessageBoxIcon.Information );
      }
    }

    private void CreateReport( ) {
      ChildficheData data = new ChildficheData( );
      if ( !selectionRadioButton.Checked ) {
        children = childrenBll.GetChildren( allRadioButton.Checked );
        data = reportBll.GetChildficheReportData( children );
      } else {
        if ( children.Count == 0 ) {
          MessageBox.Show( "Geen selectie gemaakt.", "Selectie", MessageBoxButtons.OK, MessageBoxIcon.Warning );
        } else {
          data = reportBll.GetChildficheReportData( children );
        }
      }
      if ( data.ChildFiche.Count > 0 ) {

        ReportViewForm form = new ReportViewForm( data, "ChildFiche", "ChildficheDataset", "Presentation.Reports.Childfiches.ChildfichesReport.rdlc" );
        form.MdiParent = this.MdiParent;
        form.Show( );
        this.Close( );
      } else {
        MessageBox.Show( "Geeg gegevens gevonden.", "Afdruk kinderfiches", MessageBoxButtons.OK, MessageBoxIcon.Information );
      }
    }

    #endregion


  }
}
